<?php

use app\common\com\mail\EmailBase;

class mail_smtp extends EmailBase
{

	public function send($email, $subject, $content, $type = 0, $attachment = null)
	{
		$site_config = load_site_config();
		/* 如果邮件编码不是CHARSET，创建字符集转换对象，转换编码 */
		if (strtolower($this->config['charset']) != strtolower(CHARSET)) {
			$subject = ecs_iconv(CHARSET, $this->config['charset'], $subject);
			$content = ecs_iconv(CHARSET, $this->config['charset'], $content);
			$site_config['site_name'] = ecs_iconv(CHARSET, $this->config['charset'], $site_config['site_name']);
		}

		static $mailer = null;
		if($mailer === null) {
			import('Org.Phpmailer', COMMON_PATH);
			$mailer = new \PHPMailer();
		}

		$mailer->IsSMTP();
		$mailer->Host = $this->config['host'];
		$mailer->SMTPAuth = true;
		$mailer->Username = $this->config['id'];
		$mailer->Password = $this->config['pass'];
		$mailer->Port = $this->config['port'];

		$mailer->SetFrom($this->config['addr'], $site_config['site_name']);
		$mailer->AddReplyTo($this->config['addr'], $site_config['site_name']);
		$mailer->AddAddress($email);
		$mailer->IsHTML($type);
		if($attachment)
		{
			$mailer->AddAttachment($attachment);
		}
		$mailer->CharSet = $this->config['charset'];
		$mailer->Subject = $subject;
		$mailer->Body = $content;
		return $mailer->Send();
	}

}